<?
include "trie.class.php";
include "connectDB.php";
$className = "Trie";
$trie = new $className();
ini_set('memory_limit','512M');
$post;
$time_start = microtime_float();

$query1 = 'select * from so_posts';
	$result1 = mysql_query($query1) or die(mysql_error());
	$tot = mysql_num_rows($result1);
	$f=0;
	while($posts = mysql_fetch_array($result1)){
	$post[++$f] = $posts['Body'];
	}
$query2 = 'select page_title from page where page_namespace=14 limit 2';
$result2 = mysql_query($query2) or die(mysql_error());
$total_concepts = mysql_num_rows($result2);
$m=0;
$c=0;
$concepts = array();
//$concepts = array_fill(0,400000, 0);
$matched;
$topic;
$g=0;
$c=0;

		//echo $concept.'<br>';
		//if(trim($post) != null){
		//if(substr_count($post, $concept)) $matches++;;
		//echo substr_count($post, 'linux');
		
		foreach($post as $p){
			$matches=0;
			$sent = $p;
			$att = explode(" ",$sent);
			$j=0;
			foreach($att as $word){
					$trie->add($word,$j);
					$j++;
				}
			while($categories = mysql_fetch_array($result2)){
				//if($matches>0) break;
				$concept = strtolower(str_replace('_',' ',$categories['page_title']));

				$tosearch = $concept;
				$ts= explode(" ",$tosearch);

				//print_r($att);
				
				
				$f=0;
				$flag=1;
				$ll=$trie->search($ts[0]);
				if($ll){
				foreach($ts as $s){
					if($ll==$trie->search($s))
						$ll++;
					else {$flag=0;break;}

				}
				}
				else
				$flag=0;
				if($flag) {echo 'dd';$matches++;}
			}
				if($matches > 0) {
					$c++;
				}
		}
	//echo $concept;
	//$c++;	

$time_end = microtime_float();
$time = $time_end - $time_start;
echo '<h1>'.$c.'</h1>';
echo '<br>'.$time.'sec';

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
?>